package task.manager;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * The account viewer class for the task manager.
 * 
 * @author Harrison Saylor, Aaron Mead, Marc Huet, Greg Ruskin
 * @version 1.0
 */
public class AccountView extends Activity {
	
	// Buttons.
	private Button btnCancel;
	private Button btnSubmit;
	
	// Confirmation label.
	private TextView lblConfirmation;
	
	// Username/password.
	private EditText etDesiredUsername;
	private EditText etDesiredPassword;
	private EditText etConfirmPassword;
	
	private LoginFacade loginFacade;
	
	/** 
	 * Called when the activity is started. 
	 * 
	 * @param savedInstanceState Any saved instance state.
	 */
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accountview);
        
        loginFacade = LoginFacade.getInstance();
        
        // Instantiate variables.
        btnCancel = (Button)findViewById(R.id.cancel2);
        btnSubmit = (Button)findViewById(R.id.button1);
        lblConfirmation = (TextView)findViewById(R.id.accountcreated);
        etDesiredUsername = (EditText)findViewById(R.id.editText1);
        etDesiredPassword = (EditText)findViewById(R.id.editText2);
        etConfirmPassword = (EditText)findViewById(R.id.editText3);
        
        /**
         * Binding Click event to the submit button.
         */
        btnSubmit.setOnClickListener(new OnClickListener() {     	
        	public void onClick(View v) {
        		
        		// Variables to check username and password.
        		String p1 = etDesiredPassword.getText().toString();
    	  		String p2 = etConfirmPassword.getText().toString();
        		String username = etDesiredUsername.getText().toString();
        		
        		if(username.length() > 0){ // If the user has entered a username.
        			if(p1.equals(p2)){ // If the passwords match.
        				loginFacade.registerNewUser(username, p1);
        				lblConfirmation.setText("Account created");
        			} // End of if statement.
        			else{ // Else the password is wrong.
        				lblConfirmation.setText("Passwords do not match");
        			} // End of else statement.
        		} // End of if statement.
        		else{ // Else the user has not entered a username.
        			lblConfirmation.setText("Please enter a username");
        		} // End of else statement.
        		
        	} // End of onClick method.
        }); // End of setOnClickListener event.
        
        /** 
         * Binding Click event to cancel button 
         */
        btnCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Closes activity.
                finish();
            } // End of onClick method.
        }); // End of setOnClickListener event.
        
	} // End of onCreate method.
	
	@Override
	public void onPause() {
		super.onPause();
		try {
			loginFacade.saveUsers();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
} // End of AccountView class.
